package com.lz.ionicbase.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.lz.ionicbase.R;
import com.lz.ionicbase.model.GridListBean;
import com.lz.ionicbase.ui.activity.BaseWebViewActivity;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.SpUtils;
import com.lz.ionicbase.view.MyGridAdapter;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by lenovo on 2017/2/4.
 */
public class AppFragment extends BaseFragment {

    @InjectView(R.id.app_gridview)
    GridView appGridview;

    @InjectView(R.id.app_addapp)
    ImageView appAddapp;
    private SpUtils sp;
    private TelephonyManager telephonyManager;
    public List<GridListBean> list;
    private MyGridAdapter adapter;
    private String netUrl;
    private static boolean isShowDelete;
    private boolean isClick = true;
    public String mJson;
    private String token;
    private String deviceId;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_app, null);
        ButterKnife.inject(this, view);
        sp = new SpUtils();
        initView();
        return view;
    }

    private void initView() {
        isShowDelete = false;
    }

    private void initData() {
        token = sp.getString(getActivity(), Constant.TOKEN, "");
        telephonyManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        deviceId = telephonyManager.getDeviceId();


    }

    private void initListener() {
        appGridview.setSelector(new ColorDrawable(getResources().getColor(R.color.clickitem)));
        appGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (isShowDelete) {
                    isClick = false;
                } else {
                    isClick = true;
                }
                String sname = list.get(position).getSname();
                Toast.makeText(getActivity(), sname, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), BaseWebViewActivity.class);
                intent.putExtra("url", netUrl + list.get(position).getSurl());
                if (isClick) {
                    startActivity(intent);
                }
            }
        });

        appGridview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (isShowDelete) {
                    isShowDelete = false;
                } else {
                    isShowDelete = true;
                }
                adapter.setShowDelete(isShowDelete);
                return true;
            }
        });
    }

    public static void setShowDelete(boolean isDelete) {
        isShowDelete = isDelete;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
        initListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.app_addapp, R.id.app_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.app_addapp:
                break;
            case R.id.app_search:
                break;
        }
    }

}
